<?php
/**
 * Created by PhpStorm.
 * User: 13095
 * Date: 2018/9/14
 * Time: 10:56
 */

namespace app\api\controller;


use app\api\validate\ValidateAuth;
use app\common\controller\Api;
use think\Config;
use think\Controller;
use think\Exception;
use think\Request;

class Auth extends Controller
{
    protected $data;
    protected $model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = model('Auth');
    }

    public function  lists()
    {
        $this->data['where'] = array();
        $this->data['field'] = 'id,auth_name,auth_rules,is_tyle,create_time';
        $this->data['order'] = 'id asc';
        $this->data['page'] = $_GET['page'];
        $this->data['limit'] = $_GET['limit'];
        $this->data['data'] = $this->model->lists($this->data);
        Api::returnJson('0', $this->data['data'], $this->model->where($this->data['where'])->count());
    }

    public function add()
    {
        if($this->request->isPost()){
            $this->data['data'] = $_POST;
            if($this->data['data']){
                $validateAuth = new ValidateAuth();
                if(!$validateAuth->scene('add')->check($this->data['data'])){
                    //$this->error($validateAuth->getError());
                    Api::returnJson('200', [], $validateAuth->getError());
                }
                try{
                    $res = $this->model->add($this->data['data']);
                    if($res === true){
                        Api::returnJson('200', [], '新增成功');
                    }
                    throw  new Exception($res);
                }catch (Exception $e)
                {
                    Api::returnJson('500', [], $e->getMessage());
                }
            }else{
                Api::returnJson('500', [], '数据不存在');
            }
        }
    }

    public function edit()
    {
        if($this->request->isPost()){
            $this->data['data'] = $_POST;
            $this->data['where'] = array('id'=>$this->data['data']['ids']);
            if($this->data['data']){
                $validateAuth = new ValidateAuth();
                if(!$validateAuth->scene('edit')->check($this->data['data'])){
                    Api::returnJson('500', [], $validateAuth->getError());
                }
                try{
                    $res = $this->model->edit($this->data);
                    if($res === true){
                        Api::returnJson('200', [], '编辑成功');
                    }
                    throw  new Exception($res);
                }catch (Exception $e)
                {
                    Api::returnJson('500', [], $e->getMessage());
                }
            }else{
                Api::returnJson('500', [], '数据不存在');
            }
        }
    }

    public function del($ids)
    {
        if(empty($ids)){
            Api::returnJson('500',[],lang('data empty'));
        }
        $res = $this->model->where(['id'=>$ids])->delete();
        if($res){
            Api::returnJson('200',[],lang('Operation is successful'));
        }
        Api::returnJson('500',[],lang('The operation failure'));
    }
}